package org.example.page;

import lombok.extern.log4j.Log4j2;
import org.example.param.Config;
import org.example.param.Parameter;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Properties;

@Log4j2
public class MainPanel extends JPanel implements ActionListener {
    private CardLayout cardLayout;
    private JPanel cardPanel;
    private Properties config;

    public MainPanel(CardLayout cardLayout,JPanel cardPanel) {
        this.cardPanel = cardPanel;
        this.cardLayout = cardLayout;
        configInit();

        panelInit();

        //按钮初始化
        btnInit();

    }

    private void configInit() {
        config = Config.getConfig();
    }

    private void btnInit() {
        JButton signatureBtn = new JButton(Parameter.SIGNATURE_BTN);
        JButton recordBtn = new JButton(Parameter.RECORD_BTN);
        JButton batchDeleteBtn = new JButton(Parameter.BATCH_DELETE);
        JButton frameMoveBtn = new JButton(Parameter.FRAME_MOVE);
        JButton windowsUpdateBtn = new JButton(Parameter.WINDOWS_UPDATE_BTN);
        JButton chatBtn = new JButton(Parameter.CHAT);

        signatureBtn.addActionListener(this);
        recordBtn.addActionListener(this);
        batchDeleteBtn.addActionListener(this);
        frameMoveBtn.addActionListener(this);
        windowsUpdateBtn.addActionListener(this);
        chatBtn.addActionListener(this);

        this.add(signatureBtn);
        this.add(recordBtn);
        this.add(batchDeleteBtn);
        this.add(frameMoveBtn);
        this.add(windowsUpdateBtn);
        this.add(chatBtn);
    }

    private void panelInit() {
        GridLayout gridLayout = new GridLayout(3, 2);
        this.setLayout(gridLayout);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() instanceof JButton){
            JButton btn = (JButton) e.getSource();
            String btnText = btn.getText();
            switch (btnText){
                case Parameter.SIGNATURE_BTN :
                    cardLayout.show(cardPanel,"signaturePanel");
                    break;
                case Parameter.RECORD_BTN:
                    cardLayout.show(cardPanel,"eventReplay");
                    break;
                case Parameter.BATCH_DELETE:
                    //批量删除
                    cardLayout.show(cardPanel,"batchDeletePanel");
                    break;
                case Parameter.FRAME_MOVE:
                    cardLayout.show(cardPanel,"frameMovePanel");
                    break;
                case Parameter.WINDOWS_UPDATE_BTN:
                    //展示页面
                    showWindowsUpdatePage();
                case Parameter.CHAT:
//                    log.info("聊天按钮背点击");
                    showChatPanel();
                    break;
            }
        }




    }

    private void showChatPanel() {
        if (!"true".equals((String) config.get("chat"))){
            log.info("不能使用聊天，如果想使用请修改配置，然后重新启动");
            return;
        }

        cardLayout.show(cardPanel,"chatPanel");
    }

    private void showWindowsUpdatePage() {
        WindowsErrPanel windowsErrPanel = new WindowsErrPanel();
        windowsErrPanel.showWindowsUpdatePage();
    }
}
